package com.dexilaonian;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.dexilaonian.fragment.DoorServiceFragment;
import com.dexilaonian.fragment.HomeFragment;
import com.dexilaonian.fragment.MeFragment;
import com.dexilaonian.fragment.StoreFragment;
import com.dexilaonian.ui.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.fl_content)
    FrameLayout flContent;
    @Bind(R.id.fragment_tabhost)
    FragmentTabHost tahost;
    private String TextArray[] = {"孝孝", "上门服务", "商城", "我"};
  //private int TextArray[] = {R.drawable.selector_text, R.drawable.selector_text, R.drawable.selector_text1, R.drawable.selector_text};
    private int mImageArray[] = {R.drawable.selector_home, R.drawable.selector_door, R.drawable.selector_store,  R.drawable.selector_bar};
    private Class FragmentActivity[] = {HomeFragment.class, DoorServiceFragment.class, StoreFragment.class,MeFragment.class};
    private LayoutInflater layoutInflater;
    private int id;
    private static MainActivity mainActivity;
    static String selectCity = "";
    private boolean isExit = false;
    public static Context getContext() {
        return mainActivity;
    }

    @Override
    protected void initData() {
    }

    @Override
    public void initView() {

    }
    @Override
    public int getLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
      mainActivity = MainActivity.this;
      init();
      Intent intent = getIntent();
        if (intent != null) {
            Bundle extras = intent.getExtras();
            if (extras != null) {
                selectCity = extras.getString("SELECT");
            }
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        int user = getIntent().getIntExtra("user", 0);
        if (user==1) {
        MeFragment meFragment = new MeFragment();
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fl_content, meFragment);
        fragmentTransaction.commit();
        finish();
        } else if (user == 2) {
            setCurrentTab(2);
        }
    }
    //记录用户首次点击返回键的时间
/*    private long firstTime=0;
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode){
            case KeyEvent.KEYCODE_BACK:
                long secondTime=System.currentTimeMillis();
                if(secondTime-firstTime>2000){
                    Toast.makeText(MainActivity.this,"再按一次退出程序",Toast.LENGTH_SHORT).show();
                    firstTime=secondTime;
                    return true;
                }else{
                    System.exit(0);
                }
                break;
        }
        return super.onKeyUp(keyCode, event);
    }*/
//主线程处理视图，isExit默认为false，就是点击第一次时，弹出"再按一次退出程序"
    //点击第二次时关闭应用
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 点击两次退出程序
     */
    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            // 利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
            //参数用作状态码；根据惯例，非 0 的状态码表示异常终止。
            System.exit(0);
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == 12) {
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
   public String getCity() {
        return selectCity;
    }

    private void init() {
        layoutInflater = LayoutInflater.from(this);
        tahost.setup(this, getSupportFragmentManager(), R.id.fl_content);
 /*       if (!woDeFragment.isAdded()) {
            getFragmentManager().beginTransaction().replace(R.id.cont, woDeFragment).commit();
        }*/
        for (int i = 0; i < FragmentActivity.length; i++) {
            TabHost.TabSpec tabSpec = tahost.newTabSpec(TextArray[i]).setIndicator(getTabItemView(i));
            tahost.addTab(tabSpec, FragmentActivity[i], null);
            tahost.getTabWidget().setDividerDrawable(null);
            View view = tahost.getTabWidget().getChildAt(i);
            tahost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.shape_un);
            view.setId(i);
            view.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View view) {
        setCurrentTab(view.getId());
    }

    public void setCurrentTab(int index)   {
        tahost.setCurrentTab(index);
    }

    private View getTabItemView(int i) {
        View view = layoutInflater.inflate(R.layout.tab_item_view, null);
        ImageView iv_images = (ImageView) view.findViewById(R.id.iv_image);
        iv_images.setImageResource(mImageArray[i]);

        TextView mytext = (TextView) view.findViewById(R.id.mytext);
        mytext.setText(TextArray[i]);
        return view;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}